//
//  UIView+Extension.m
//  01-黑酷
//
//  Created by apple on 14-6-27.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "UIView+Extension.h"
#import <objc/runtime.h>
@implementation UIView (Extension)

static const void *kshowNoView = @"kshowNoView";

- (void)setX:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x {
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y {
    return self.frame.origin.y;
}

- (void)setWidth:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width {
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height {
    return self.frame.size.height;
}

- (void)setSize:(CGSize)size {
//    self.width = size.width;
//    self.height = size.height;
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size {
    return self.frame.size;
}

- (void)setOrigin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGPoint)origin {
    return self.frame.origin;
}

- (void)setCenterX:(CGFloat)centerX {
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX {
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY {
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY {
    return self.center.y;
}


- (CGFloat)left {
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)top {
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)right {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight:(CGFloat)right {
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)bottom {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom:(CGFloat)bottom {
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}

- (void)setBorderColor:(UIColor *)borderColor {
    self.layer.borderColor = borderColor.CGColor;
}

- (UIColor *)borderColor {
    return [UIColor colorWithCGColor:self.layer.borderColor];
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    self.layer.borderWidth = borderWidth;
}

- (CGFloat)borderWidth {
    return self.layer.borderWidth;
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = YES;
    
    // 栅格化 - 提高性能
    // 设置栅格化后，图层会被渲染成图片，并且缓存，再次使用时，不会重新渲染
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    self.layer.shouldRasterize = YES;
}

- (CGFloat)cornerRadius {
    return self.layer.cornerRadius;
}

-(NSNumber *)leftTopCorner{
    return objc_getAssociatedObject(self, _cmd);
}
-(NSNumber*)rightTopCorner{
    return objc_getAssociatedObject(self, _cmd);
}
-(NSNumber*)leftBottomCorner{
    return objc_getAssociatedObject(self, _cmd);
}
-(NSNumber*)rightBottomCorner{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setLeftTopCorner:(NSNumber *)leftTopCorner{
    objc_setAssociatedObject(self, @selector(leftTopCorner), leftTopCorner, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self drawLine:CGSizeMake([self.leftTopCorner integerValue], [self.leftTopCorner integerValue]) corner:UIRectCornerTopLeft];
}
- (void)setRightTopCorner:(NSNumber *)rightTopCorner{
    objc_setAssociatedObject(self, @selector(rightTopCorner), rightTopCorner, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self drawLine:CGSizeMake([self.rightTopCorner integerValue], [self.rightTopCorner integerValue]) corner:UIRectCornerTopRight];
}
- (void)setLeftBottomCorner:(NSNumber *)leftBottomCorner{
    objc_setAssociatedObject(self, @selector(leftBottomCorner), leftBottomCorner, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self drawLine:CGSizeMake([self.leftBottomCorner integerValue], [self.leftBottomCorner integerValue]) corner:UIRectCornerBottomLeft];
}
- (void)setRightBottomCorner:(NSNumber *)rightBottomCorner{
    objc_setAssociatedObject(self, @selector(rightBottomCorner), rightBottomCorner, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self drawLine:CGSizeMake([self.rightBottomCorner integerValue], [self.rightBottomCorner integerValue]) corner:UIRectCornerBottomRight];
}
- (NSNumber *)left_right_topCorner{
    return objc_getAssociatedObject(self, _cmd);
}
- (NSNumber *)left_right_bottomCorner{
    return objc_getAssociatedObject(self, _cmd);
}
- (NSNumber *)left_top_bottomCorner{
    return objc_getAssociatedObject(self, _cmd);
}
- (NSNumber *)right_top_bottomCorner{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setLeft_right_topCorner:(NSNumber *)left_right_topCorner{
    objc_setAssociatedObject(self, @selector(left_right_topCorner), left_right_topCorner, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self drawLine:CGSizeMake([self.left_right_topCorner integerValue], [self.left_right_topCorner integerValue]) corner:UIRectCornerTopLeft|UIRectCornerTopRight];
}
- (void)setLeft_right_bottomCorner:(NSNumber *)left_right_bottomCorner{
    objc_setAssociatedObject(self, @selector(left_right_bottomCorner), left_right_bottomCorner, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self drawLine:CGSizeMake([self.left_right_bottomCorner integerValue], [self.left_right_bottomCorner integerValue]) corner:UIRectCornerBottomLeft|UIRectCornerBottomRight];
}
- (void)setLeft_top_bottomCorner:(NSNumber *)left_top_bottomCorner{
    objc_setAssociatedObject(self, @selector(left_top_bottomCorner), left_top_bottomCorner, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self drawLine:CGSizeMake([self.left_top_bottomCorner integerValue], [self.left_top_bottomCorner integerValue]) corner:UIRectCornerTopLeft|UIRectCornerBottomLeft];
}
- (void)setRight_top_bottomCorner:(NSNumber *)right_top_bottomCorner{
    objc_setAssociatedObject(self, @selector(right_top_bottomCorner), right_top_bottomCorner, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self drawLine:CGSizeMake([self.right_top_bottomCorner integerValue], [self.right_top_bottomCorner integerValue]) corner:UIRectCornerTopRight|UIRectCornerBottomRight];
}
-(void)drawLine:(CGSize)s corner:(UIRectCorner)c{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)) byRoundingCorners:c cornerRadii:s];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

- (void)addTarget:(id)target action:(SEL)action;
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:target
                                                                         action:action];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];
}
- (UIViewController*)getCurrentViewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}
//获得某个范围内的屏幕图像
+ (UIImage *) getCurrentInnerViewShot: (UIView *) innerView atFrame:(CGRect)rect
{
    UIGraphicsBeginImageContext(innerView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    UIRectClip(rect);
    [innerView.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return  theImage;
}
-(UIImage * _Nullable)view2Image{
    CGRect rect = self.frame;
    if ([self isKindOfClass:[UIScrollView class]]) {
        rect.size = ((UIScrollView *)self).contentSize;
    }
    UIGraphicsImageRenderer * render = [[UIGraphicsImageRenderer alloc]initWithSize:rect.size];
    UIImage * image = [render imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
        if ([self isKindOfClass:[UIScrollView class]]) {
            UIScrollView * scrollView = (UIScrollView *)self;
            CGPoint savedContentOffset = scrollView.contentOffset;
            CGRect savedFrame = scrollView.frame;
            scrollView.contentOffset = CGPointZero;
            scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
            [scrollView.layer renderInContext:rendererContext.CGContext];
            scrollView.contentOffset = savedContentOffset;
            scrollView.frame = savedFrame;
        }else{
            [self.layer renderInContext:rendererContext.CGContext];
        }
    }];
    return image?:nil;
}

#pragma mark 无数据视图
-(UIImageView *)im{
        UIImageView *im = [[UIImageView alloc]init];
        im.image = [UIImage imageNamed:@"暂无数据"];
        im.contentMode =UIViewContentModeCenter;
    
    return im  ;
}
-(UILabel *)label{
      UILabel  *_label = [[UILabel alloc]init];
        _label.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:153/255.0];
        _label.font = [UIFont systemFontOfSize:15];
        _label.text = @"暂无数据";
        _label.numberOfLines = 0;
        _label.textAlignment = NSTextAlignmentCenter;
    return _label;
}
-(UIView *)containerV{
    UIView *v = [[UIView alloc]init];
    v.tag = 8808;
//    v.backgroundColor = [UIColor redColor];
    return v;
    
}


#pragma mark - BOOL类型的动态绑定
- (BOOL)showNoView {
    return [objc_getAssociatedObject(self, kshowNoView) boolValue];
}
-(BOOL)isShowNoView{
    return [objc_getAssociatedObject(self, kshowNoView) boolValue];
}
- (void)setShowNoView:(BOOL)showNoView {
    objc_setAssociatedObject(self, kshowNoView, [NSNumber numberWithBool:showNoView], OBJC_ASSOCIATION_ASSIGN);
}

-(void)showNoView:(NSString *_Nonnull)title image:(UIImage *_Nullable)placeImage certer:(CGPoint)p{
    
    
    UIView *containerV = [self containerV];
    
    containerV.width =[UIScreen mainScreen].bounds.size.width;
//    containerV.height = 100;
    UILabel *label = [self label];
    UIImageView *iv = [self im];
    
    if (title) {
         label.text = title;
    }
    if (placeImage) {
         iv.image =placeImage;
    }

    
    
    
    iv.size = iv.image.size;
    iv.y = 0;
    iv.centerX = containerV.width/2.0;
    label.width =containerV.width;
    [label sizeToFit];
    label.centerX = containerV.width/2.0;
    label.y = CGRectGetMaxY(iv.frame)+12;
    
    containerV.height = CGRectGetMaxY(label.frame);
    if (p.x>0&&p.y>0) {
        containerV.center = p;
    }else{
        containerV.x = 0;
        containerV.y = (self.height - containerV.height)/2.0;
    }
    
    [containerV addSubview:iv];
    [containerV addSubview:label];
    [self addSubview: containerV];
    [self setShowNoView:YES];
    
}
-(void)dismissNoView{
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.tag == 8808) {
            [obj removeFromSuperview];
            [self setShowNoView:NO];
        }
    }];
}
@end
